package com.qfedu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.BoatEntity;
import com.qfedu.entity.BoatTypeEntity;
import com.qfedu.entity.RouteEntity;
import com.qfedu.service.BoatService;
import com.qfedu.service.RouteService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class BoatController {

    @Reference(check = false)
    BoatService boatService;

    @Reference(check = false)
    RouteService routeService;

    @RequestMapping("jumpBoat")
    public String  jumpRoute(HttpServletRequest request, BoatEntity be){


        PageInfo<BoatEntity> pageInfo = boatService.queryBoatList(be);


        request.setAttribute("boatlist",pageInfo);
        request.setAttribute("boat",be);

        return "boat";
    }


    @RequestMapping("/delBoat")
    @ResponseBody
    public String del(int sid){

        boolean del = boatService.delBoat(sid);

        if (del){
            return "0000";
        }
        return "1111";
    }



    @RequestMapping("/delAllBoat")
    @ResponseBody
    public String  delAll(String ids){

        boolean b = boatService.deAllBoat(ids);

        return "0000";
    }



    @RequestMapping("/updateState")
    @ResponseBody
    public String updateState(BoatEntity bee){

        BoatEntity be = boatService.queryBoatById(bee.getSid());

        if (be.getSstate() == 1){
            be.setSstate(2);
        }else  {
            be.setSstate(1);
        }

        boolean update = boatService.updateBoat(be);


        if (be.getSstate() == 1){
            return "1";
        }else {
            return "2";
        }

    }



    @RequestMapping("/jumpBoatTan")
    public String  jumpBoatTan(HttpServletRequest request){

        List<RouteEntity> routeList = routeService.queryRouteList();

        List<BoatTypeEntity> boatTypeList = boatService.queryBoatType();

        request.setAttribute("rnameList",routeList);

        request.setAttribute("boattype",boatTypeList);


        return "addBoat";
    }


    @RequestMapping("/addBoat")
    @ResponseBody
    public String addBoat(BoatEntity be){

        boatService.insertBoat(be);

        return "0000";
    }



    @RequestMapping("updateBoatId")
    @ResponseBody
    public String  updateBoatId(BoatEntity be,HttpServletRequest request){
        request.getSession().setAttribute("sid",be);

        return "0000";
    }


    @RequestMapping("/jumpUpdateBoat")
    public String jumpUpdateBoat(HttpServletRequest request){

        BoatEntity sid = (BoatEntity) request.getSession().getAttribute("sid");

        BoatEntity boatEntity = boatService.queryBoatById(sid.getSid());

        request.getSession().removeAttribute("sid");

        List<BoatTypeEntity> boatTypeEntities = boatService.queryBoatType();

        List<RouteEntity> routeEntityList = routeService.queryRouteList();

        request.setAttribute("nowboat",boatEntity);
        request.setAttribute("nowboattype",boatTypeEntities);
        request.setAttribute("nowroute",routeEntityList);

        return "updateBoat";

    }



    @RequestMapping("/updateBoat")
    @ResponseBody
    public String updateBoat(BoatEntity be){

        boatService.updateBoat(be);

        return "0000";
    }
}
